package com.blue.core.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.Assert;

/**
 * 字符串工具类
 * 
 * @author zhengj
 * @since 1.0
 * @date 2009-2-15
 * @date 1.0
 */
public class StringUtil
{
	public static final String UTF8 = "UTF-8";

	private StringUtil()
	{
	}
	
	//private static Pattern scriptRegEx = Pattern.compile("<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>", Pattern.CASE_INSENSITIVE);
	//private static Pattern styleRegEx = Pattern.compile("<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>", Pattern.CASE_INSENSITIVE);
	private static Pattern htmlRegEx = Pattern.compile("<[^>]+>", Pattern.CASE_INSENSITIVE);
	
	/**
	 * 过滤HTML标签
	 * 
	 * @param html 带HTML标签的文本
	 * @return 纯文本
	 */
	public static String html2Text(String html)
	{
		String text = html;
		Matcher matcher = htmlRegEx.matcher(text);
		text = matcher.replaceAll("");
		
		return text;
	}
	
	private static Pattern scriptRegex = Pattern.compile("<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>", Pattern.CASE_INSENSITIVE);
	private static Pattern commentRegex = Pattern.compile("<!--.*-->", Pattern.CASE_INSENSITIVE);
	private static Pattern styleRegex = Pattern.compile("<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>", Pattern.CASE_INSENSITIVE);
	private static Pattern selectRegex = Pattern.compile("<select[^>]*>.*</select>", Pattern.CASE_INSENSITIVE);
	private static Pattern textRegex = Pattern.compile("<textarea[^>]*>.*</textarea>", Pattern.CASE_INSENSITIVE);
	private static Pattern inputRegex = Pattern.compile("</?input[^>]*>", Pattern.CASE_INSENSITIVE);
	private static Pattern fontRegex = Pattern.compile("</?font[^>]*>", Pattern.CASE_INSENSITIVE);
	private static Pattern aRegex = Pattern.compile("</?a[^>]*>", Pattern.CASE_INSENSITIVE);
	private static Pattern spanRegex = Pattern.compile("</?span[^>]*>", Pattern.CASE_INSENSITIVE);
	private static Pattern iframeRegex = Pattern.compile("</?iframe[^>]*>", Pattern.CASE_INSENSITIVE);
	/**
	 * 去除一部分的html标记
	 * 
	 * @param html
	 * @return
	 */
	public static String htmlTrim(String html)
	{
		Matcher m = scriptRegex.matcher(html);
		html = m.replaceAll("");
		m = commentRegex.matcher(html);
		html = m.replaceAll("");
		m = styleRegex.matcher(html);
		html = m.replaceAll("");
		m = selectRegex.matcher(html);
		html = m.replaceAll("");
		m = textRegex.matcher(html);
		html = m.replaceAll("");
		m = inputRegex.matcher(html);
		html = m.replaceAll("");
		m = fontRegex.matcher(html);
		html = m.replaceAll("");
		m = aRegex.matcher(html);
		html = m.replaceAll("");
		m = spanRegex.matcher(html);
		html = m.replaceAll("");
		m = iframeRegex.matcher(html);
		html = m.replaceAll("");

		return html.trim();
	}
	
	/**
	 * 在字符串上加上掩码
	 * 
	 * @param src 原字符串
	 * @param start 开始位置，从0开始
	 * @param length 替换长度
	 * @param mark 掩码
	 * @return 加上掩码后的字符串
	 */
	public static String mask(String src, int start, int length, String mark)
	{
		int len = src.length();
		if (len <= start)
			return src;
		
		StringBuilder sb = new StringBuilder(src.substring(0, start));
		for (int i = 0; i < Math.ceil((double)length / mark.length()); i++)
			sb.append(mark);
		
		if (len > start + length)
			sb.append(src.substring(start + length));
		
		return sb.toString();
		
	}
	
	/**
	 * 生成字符串序列
	 * 
	 * @param prefix 前缀
	 * @param suffix 后缀
	 * @param start 开始序号
	 * @param end 结束序号
	 * @param length 序列长度
	 * @return 字符串序列
	 */
	public static List<String> sequence(String prefix, String suffix, int start, int end, int length)
	{
		if (start < 0 || end < 0)
			throw new IllegalArgumentException("start 或 end 不能小于 0");
		if (start > end)
			throw new IllegalArgumentException("start 不能大于 end");
		
		prefix = (prefix == null ? "" : prefix);
		suffix = (suffix == null ? "" : suffix);
		List<String> list = new ArrayList<>();
		for (int i = start; i <= end; i++)
		{
			StringBuilder seq = new StringBuilder(prefix.length() + suffix.length() + length);
			seq.append(prefix);
			String val = String.valueOf(i);
			int j = length - val.length();
			while (j-- > 0)
			{
				seq.append("0");
			}
			seq.append(val);
			list.add(seq.toString());
			seq.append(suffix);
		}
		return list;
	}
	
	/**
	 * 根据模板生成字符串
	 * 
	 * @param template 模板
	 * @param param 参数
	 * @return 生成的字符串
	 */
	public static String template(String template, Map<String, String> param)
	{
		return template(template, param, null, null);
	}
	
	/**
	 * 根据模板生成字符串
	 * 
	 * @param template 模板
	 * @param param 参数
	 * @param prefix 前缀
	 * @param suffix 后缀
	 * @return 生成的字符串
	 */
	public static String template(String template, Map<String, String> param, String prefix, String suffix)
	{
		Assert.hasText(template, "模板不能为空");
		Assert.notEmpty(param, "参数不能为空");
		StringBuilder content = new StringBuilder(template);
		for (Map.Entry<String, String> entry : param.entrySet())
		{
			StringBuilder p = new StringBuilder(32);
			if (prefix != null && !prefix.isEmpty())
			{
				p.append(prefix);
			}
			p.append(entry.getKey());
			if (suffix != null && !suffix.isEmpty())
			{
				p.append(suffix);
			}
			int index = 0;
			while (index != -1)
			{
				index = content.indexOf(p.toString());
				if (index != -1)
				{
					content.replace(index, index + p.length(), entry.getValue());
				}
			}
		}
		return content.toString();
	}
	
	public static void main(String[] args)
	{
		String src = "12345678901";
		String mark = "*";
		System.out.println(mask(src, 4, 4, mark));
	}
}
